#ifndef __GOLDSHIELD_GOLDDHIELD_DOWNLOADER_H__
#define __GOLDSHIELD_GOLDDHIELD_DOWNLOADER_H__
#include <queue>
#include <string>

class CDownloader
{
	enum { TIMER_ID = 0xdeb };
public:
	CDownloader()
		: downloading_(false)
	{
		InitializeCriticalSection(&critical_section_);
		InitializeCriticalSection(&critical_section_msg_);
		this_ = this;
		CoInitialize(NULL);
	}
	~CDownloader()
	{
		CoUninitialize();
	}
public:
	inline std::wstring& get_client_url() { return client_url_; }
	inline std::wstring& get_file_name() { return file_name_; }
	static inline CDownloader* get_this(){ return this_; }
public:
	BOOL IsClientInstalled();
	bool DownloadAndInstallClient(const wchar_t* client_url, const wchar_t* branch_name);
	void PushMsg(const wchar_t* msg);
	void OutPutMsg();
private:
	void Lock();
	void UnLock();
	bool InstallClient();
	void OutPutStatusText(const wchar_t* format, ...);
	static DWORD WINAPI DownloadThread(LPVOID lpParameter);
	static VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
private:
	//
	// for download
	//
	bool downloading_;
	CRITICAL_SECTION critical_section_;
	CRITICAL_SECTION critical_section_msg_;
	std::wstring client_url_;
	std::wstring file_name_;
	std::queue<std::wstring> msg_queue_;
	static CDownloader* this_;
};
#endif//__GOLDSHIELD_GOLDDHIELD_DOWNLOADER_H__
